//
//  TopicDetailTableViewCell.m
//  yilutongxing
//
//  Created by ios-21 on 17/4/13.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "TopicDetailTableViewCell.h"
#import "TopicDetailViewController.h"
@implementation TopicDetailTableViewCell
- (IBAction)nameBtnAction:(UIButton *)sender {
    if ([_delegate respondsToSelector:@selector(jumpToUserInformaintionWithButtonTag:)]) {
        [_delegate jumpToUserInformaintionWithButtonTag:sender.tag];
    }
}
- (IBAction)commentAction:(UIButton *)sender {
    if ([_delegate respondsToSelector:@selector(commentWithButtonTag:)]) {
        [_delegate commentWithButtonTag:sender.tag];
    }
}
- (IBAction)deleteAction:(UIButton *)sender {
    if ([_delegate respondsToSelector:@selector(deleteWithButtonTag:)]) {
        [_delegate deleteWithButtonTag:sender.tag];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImgBtn.layer.cornerRadius = 15;
    self.headImgBtn.layer.masksToBounds = YES;
    self.myTextV = [YYTextView new];
    
    self.commentBtn.frame = CGRectMake(SCREEN_WIDTH - 35, 15, 15, 15);
    
    self.headImgBtn.frame = CGRectMake(20, 9, 30, 30);
    self.userNameLable.frame = CGRectMake(CGRectGetMaxX(self.headImgBtn.frame)+10, 15, 100, 13);
    self.userNameBtn.frame = self.userNameLable.frame;
    
    self.myTextV.frame = CGRectMake(self.creadDateLable.frame.origin.x, self.creadDateLable.frame.origin.y+self.creadDateLable.frame.size.height+11,SCREEN_WIDTH-self.creadDateLable.frame.origin.x-20, 1000);
    self.myTextV.editable = NO;
    [self addSubview:self.myTextV];
    self.imgView = [UIImageView new];
    self.deletTopicBtn.frame = CGRectMake(SCREEN_WIDTH - 42,
                                       self.myTextV.frame.origin.y + self.myTextV.frame.size.height - 25,
                                       28,
                                       22);
    //[self.contentView bringSubviewToFront:self.deletTopicBtn];
}
-(void)clickReplyNameAction{
     NSLog(@"跳转到评论者详情");
}
-(void)showDataWithUser:(RPUserInfoBean*)data AndReply:(RPUserIdInfoBean*)replyData isMine:(BOOL)ismine{
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:data.user.avatar]];
    
    [self.headImgBtn setBackgroundImage:self.imgView.image forState:UIControlStateNormal];
    
    self.userNameLable.text = data.user.firstName;
    
    self.creadDateLable.text = data.createdDate;
    if (replyData.firstName !=nil) {
        NSString *text = [NSString stringWithFormat:@"回复 %@ ：%@",replyData.firstName,data.content];
        NSMutableAttributedString *commentStr = [[self setLineSpacingWithControl:nil AndString:text AndLineSpacing:9 AndFont:13]mutableCopy];
        NSRange range = [text rangeOfString:replyData.firstName];
        //获取名字的宽度以及大小设置Button的Frame
        UILabel *lable = [[UILabel alloc]init];
        lable.text = replyData.firstName;
        [lable sizeToFit];
        CGFloat width = lable.frame.size.width;
        CGFloat height = lable.frame.size.height;
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(84, 60, width, height)];
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:self action:@selector(clickReplyNameAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        [commentStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:127/255.0 green:181/255.0 blue:231/255.0 alpha:1.0] range:range];
        
        [commentStr yy_setTextUnderline:[YYTextDecoration decorationWithStyle:YYTextLineStyleNone] range:range];
        self.myTextV.attributedText =commentStr;
    }else{
        self.myTextV.attributedText = [self setLineSpacingWithControl:nil AndString:data.content AndLineSpacing:9 AndFont:13];
    }
    [self.myTextV sizeToFit];
    self.height = self.myTextV.frame.origin.y+self.myTextV.frame.size.height + 15 + 22;
    
    if (ismine) {
        self.commentBtn.hidden = YES;
        self.deletTopicBtn.hidden = NO;
        CGRect rect = CGRectMake(SCREEN_WIDTH - 42,
                                 self.myTextV.frame.origin.y + self.myTextV.frame.size.height + 10,
                                 28,
                                 22);
        self.deletTopicBtn.frame = rect;
    }
    else {
        self.deletTopicBtn.hidden = YES;
        self.commentBtn.hidden = NO;
        CGRect rect = CGRectMake(SCREEN_WIDTH - 35,
                                 self.myTextV.frame.origin.y + self.myTextV.frame.size.height + 10,
                                 15,
                                 15);
        self.commentBtn.frame = rect;
    }
}
//设置字体间距
-(NSAttributedString *)setLineSpacingWithControl:(UITextView*)textView AndString:(NSString *)string AndLineSpacing:(CGFloat)lineSpacing AndFont:(CGFloat)font{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:font],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    return [[NSAttributedString alloc] initWithString:string attributes:attributes];
}
@end
